package org.gw.ylc.base.util;

import org.gw.ylc.base.context.RequestContext;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.model.UserInfo;
import org.springframework.util.StringUtils;

public class PermissionUtil {
	public static boolean isAnonymous() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			return user.isAnonymous();
		}
		return false;
	}

	public static UserInfo getCurrentUserInfo() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			return user;
		}
		return null;
	}

	public static String getCurrentUserId() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			return user.getId();
		}
		return null;
	}

	public static String getCurrentUserCode() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			return user.getUid();
		}
		return null;
	}

	public static String getFilteredDepartmentId(String department) {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.isAnonymous()) {
				return UserInfo.ANONYMOUS_UID;
			}
			if (user.getType() < 2) {
				return user.getDepartment();
			}
		}
		return department;
	}

	public static String getFilteredStudentId(String studentId) {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() < 1) {
				return user.getId();
			}
		}
		return studentId;
	}

	public static String getFilteredStudentXh(String xh) {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() < 1) {
				return user.getUid();
			}
		}
		return xh;
	}

	public static Integer getFilteredStatus(Integer status) {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() == 0) {
				return status;
			}
			if (user.getType() == 1) {
				return user.getType();
			}
			if (user.getType() > 1) {
				return null == status ? 2 : status;
			}
			return user.getType();
		}
		return status;
	}

	public static String toLikeString(String param) {
		if (StringUtils.hasText(param)) {
			param = "%" + param + "%";
		}
		return param;
	}

	public static boolean isAdmin() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() >= 1) {
				return true;
			}
			return false;
		}
		return true;
	}

	public static boolean isSchoolAdmin() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() >= 2) {
				return true;
			}
			return false;
		}
		return true;
	}

	public static boolean isSysAdmin() {
		RequestContext context = RequestContextHolder.get();
		if (context != null) {
			UserInfo user = context.getUser();
			if (user.getType() == 3) {
				return true;
			}
			return false;
		}
		return true;
	}

	public static boolean isStudent() {
		int type = RequestContextHolder.get().getUser().getType();
		return type == 0;
	}
}
